﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace teacherplanner
{  
    public partial class SetDatesForm : Form
    {
        public SetDatesForm()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dateTimePicker2.MinDate = dateTimePicker1.Value;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string myString = dateTimePicker1.Value.ToShortDateString() + " - " + dateTimePicker2.Value.ToShortDateString();
            listBox1.Items.Add(myString);
            listBox2.Items.Add(dateTimePicker2.Value);

            ListBox.ObjectCollection myDates = listBox2.Items;
            DateTime myMinDate = (DateTime)myDates[listBox2.Items.Count - 1];
            dateTimePicker1.MinDate = myMinDate.AddDays(1);
            dateTimePicker2.MinDate = myMinDate.AddDays(1);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int myInt = listBox1.SelectedIndex;
            if (myInt == -1)
            {
                return;
            }
            object myobj = listBox1.Items[myInt];
            object myobj2 = listBox2.Items[myInt];
            listBox1.Items.Remove(myobj);
            listBox2.Items.Remove(myobj2);
            ListBox.ObjectCollection myDates = listBox2.Items;
            if (listBox2.Items.Count == 0)
            {
                dateTimePicker1.MinDate = DateTime.Parse("01/01/2000");
                dateTimePicker2.MinDate = DateTime.Parse("01/01/2000");
                return;

            }
            DateTime myMinDate = (DateTime)myDates[listBox2.Items.Count - 1];
            dateTimePicker1.MinDate = myMinDate;
            dateTimePicker2.MinDate = myMinDate;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int a = 1;
            int rangeCount = listBox1.Items.Count;
            if (rangeCount < 1)
            {
                MessageBox.Show("You must add at least one date range.", "Input Error", MessageBoxButtons.OK);
                return;
            }
            DataBase.Edit.CreateTable("CREATE TABLE TeachingDays (TeachingDayID INT IDENTITY(1,1) NOT NULL, TeachingDate DATE, DayoftheWeek INT)");
            for (int i = 0; i<rangeCount; i++)
            {
                string myString = listBox1.Items[i].ToString();
                DateTime myStartDate = DateTime.Parse(myString.Substring(0, 10));
                DateTime myEndDate = DateTime.Parse(myString.Substring(13, 10));
                TimeSpan noOfDays = myEndDate.Subtract(myStartDate);
                int numDays = noOfDays.Days;
                int sunDays = Functions.myFunctions.CountSundays(myStartDate, myEndDate);
                int lenArray = numDays - sunDays + 1;
                int j = 0;
                bool singleDayFlag = false;
                string[] tableData = new string[lenArray];

                for (DateTime myCurDate = myStartDate; myCurDate < myEndDate.AddDays(1); myCurDate = myCurDate.AddDays(1))
                {
                    DayOfWeek myDay = myCurDate.DayOfWeek;
                    if (myDay != DayOfWeek.Sunday & singleDayFlag != true)
                    {
                        string myDayStr = myDay.ToString();
                        string myDateStr = myCurDate.ToShortDateString();
                        tableData[j] = @"INSERT INTO TeachingDays VALUES ('" + a + "','" + myDateStr + "','" + (int)myDay + "')";
                        a++;
                        j++;
                        if (myStartDate == myEndDate)
                        {
                            singleDayFlag = true;
                        }
                    }

                }
                DataBase.Edit.ApplySQLArray(tableData);
                SetDatesForm.ActiveForm.Hide();
                teacherplanner.DefineTimetable myForm = new teacherplanner.DefineTimetable();
                myForm.Show();
            }
        }

        private void dateTimePicker1_ValueChanged_1(object sender, EventArgs e)
        {
            dateTimePicker2.MinDate = dateTimePicker1.Value;
        }

    }
}
